/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "combo.h"

static const char class_name [] = "EGUI Widget Combo";

static int afc_easygui_internal_combo_get_template ( DynamicClass * dyn );
static int afc_easygui_internal_cb_combo ( DynamicClass * dyn );

static void afc_easygui_combo_event_activate ( GtkWidget * w, gpointer data );
static void afc_easygui_combo_event_selected ( GtkWidget * w, gpointer data );

static int afc_easygui_combo_method_add  ( DynamicClass * dyn );
static int afc_easygui_combo_method_show ( DynamicClass * dyn );
static int afc_easygui_combo_method_unique ( DynamicClass * dyn );
static int afc_easygui_combo_method_set_text ( DynamicClass * dyn );
static int afc_easygui_combo_method_get_text ( DynamicClass * dyn );

static void afc_easygui_internal_combo_free_string ( gpointer data, gpointer user_data );
static int afc_easygui_internal_combo_search_string ( gconstpointer str, gconstpointer us );
static int afc_easygui_internal_combo_add_unique ( DynamicClass * dyn, EasyGUIWidgetData * ewidget, char * text, short case_sense );

struct unique_search 
{
	char * str;
	
	short  case_sensitive;
};


DynamicClass * afc_easygui_internal_combo_new_instance ()
{
	DynamicClass * dyn = NULL;

	if ( ( dyn = afc_dynamic_class_new () ) == NULL ) {
		AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynamic class: combo" );
		return ( NULL );
	}

	afc_dynamic_class_add_method ( dyn, "get_template", NULL,  afc_easygui_internal_combo_get_template );
	afc_dynamic_class_add_method ( dyn, "open_callback", NULL,  afc_easygui_internal_cb_combo );
	afc_dynamic_class_add_method ( dyn, "enabled",	NULL, 	afc_easygui_widget_method_enabled );

	afc_dynamic_class_add_method ( dyn, "add", 		NULL,	afc_easygui_combo_method_add );
	afc_dynamic_class_add_method ( dyn, "show", 		NULL,	afc_easygui_combo_method_show );
	afc_dynamic_class_add_method ( dyn, "unique", 		NULL,	afc_easygui_combo_method_unique );
	afc_dynamic_class_add_method ( dyn, "set_text", 	NULL,	afc_easygui_combo_method_set_text );
	afc_dynamic_class_add_method ( dyn, "get_text", 	NULL,	afc_easygui_combo_method_get_text );

	return ( dyn );
}

int afc_easygui_internal_combo_del_instance ( DynamicClass * dyn )
{
	GList * list;

	// First of all, we clean the egui_items (if exists)
	list = afc_dynamic_class_get_var ( dyn, "egui_items" );

	// Free all the items in list
	g_list_foreach ( list, afc_easygui_internal_combo_free_string, NULL );

	if ( list ) g_list_free ( list );

	return ( afc_easygui_widget_standard_free ( dyn ) );
}

static int afc_easygui_internal_combo_get_template ( DynamicClass * dyn )
{
	dyn->result = "ACTIVATE/K SELECTED/K ITEMS/K";
	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_cb_combo ( DynamicClass * dyn )
{
	EasyGUI * egui = dyn->info;
	EasyGUIContext * context = egui->curr_context;
	GtkWidget * w;
	char * items;
	char * activate;
	char * selected;
	GList * itemlist = NULL;
	int afc_res = AFC_ERR_NO_ERROR;
	DynamicClass * dc;

	if ( context->curr_container == NULL ) return ( AFC_EASYGUI_ERR_NO_CONTAINER );

	afc_easygui_widget_parse_default_params ( egui );

	items 	 = afc_cmd_parser_arg_get_by_name ( egui->parser, "ITEMS" );
	activate = afc_cmd_parser_arg_get_by_name ( egui->parser, "ACTIVATE" );
	selected = afc_cmd_parser_arg_get_by_name ( egui->parser, "SELECTED" );

	if ( ( dc = afc_dynamic_class_master_new_instance ( egui->commands, "combo" ) ) == NULL )
		return ( AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynclass instance" ) );

	WIDGET_SET_CONTEXT ( dc, context );

	w = gtk_combo_new ();

	/* first try to add the widget to the current container, then, if successful, set the other attributes */
	if ( ( afc_res = afc_easygui_internal_add_widget ( context, w, dc ) ) != AFC_ERR_NO_ERROR ) return ( afc_res );


	// Parse the item list (if any)
	if ( ( itemlist = afc_easygui_widget_make_glist ( egui, items ) ) != NULL )
	{
		gtk_combo_set_popdown_strings ( GTK_COMBO ( w ), itemlist );
	}

	// add the list to the Dyn Class variable list
	afc_dynamic_class_set_var ( dc, AFC_DYNAMIC_CLASS_VAR_KIND_POINTER, "egui_items", itemlist );

	afc_easygui_widget_add_default_events ( context, dc, w );

	if ( ( afc_res = afc_easygui_widget_connect_signal ( dc, GTK_COMBO ( w )->entry, "activate", activate, NULL, (GtkSignalFunc) afc_easygui_combo_event_activate ) ) != AFC_ERR_NO_ERROR )
		return ( afc_res );

	if ( ( afc_res = afc_easygui_widget_connect_signal ( dc, GTK_COMBO ( w )->list, "selection-changed", selected, NULL, (GtkSignalFunc) afc_easygui_combo_event_selected ) ) != AFC_ERR_NO_ERROR )
		return ( afc_res ); 

	return ( afc_res );
}

// EVENT FUNCTIONS
static void afc_easygui_combo_event_activate ( GtkWidget * w, gpointer data )
{
	DynamicClass * dc = data;
	EasyGUIComboSignalFunc sig;
	const char * text;

	// Get the function pointer (forcing it to be a EasyGUIComboSignal)
	// If the function pointer does not exists, this macro will just return
	WIDGET_GET_CALLBACK_CAST ( dc, "activate", sig, EasyGUIComboSignalFunc );

	text = gtk_entry_get_text ( GTK_ENTRY ( w ) );

	fprintf ( stderr, "COMBO: text %s\n", text );
	sig ( dc, WIDGET_GET_CONTEXT ( dc ), text );
}

static void afc_easygui_combo_event_selected ( GtkWidget * w, gpointer data )
{
	DynamicClass * dc = data;
	EasyGUIComboSignalFunc sig;
	const char * text;

	printf ( "SELECTED\n" );
	// Get the function pointer (forcing it to be a EasyGUIComboSignal)
	// If the function pointer does not exists, this macro will just return
	WIDGET_GET_CALLBACK_CAST ( dc, "selection-changed", sig, EasyGUIComboSignalFunc );

	text = gtk_entry_get_text ( GTK_ENTRY ( w ) );

	sig ( dc, WIDGET_GET_CONTEXT ( dc ), text );
}

// METHODS
static int afc_easygui_combo_method_add ( DynamicClass * dyn )
{
	//EasyGUI * egui = dyn->info;
	char * w;
	EasyGUIWidgetData * ewidget;
	GList * lst;

	w = afc_array_master_first ( dyn->args );
	WIDGET_GET_EWIDGET ( dyn, ewidget );	

	lst = afc_dynamic_class_get_var ( dyn, "egui_items" );
	lst = g_list_append ( lst, afc_string_dup ( w ) );
	afc_dynamic_class_set_var ( dyn, AFC_DYNAMIC_CLASS_VAR_KIND_POINTER, "egui_items", lst );
	
	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_combo_method_show ( DynamicClass * dyn )
{
	EasyGUIWidgetData * ewidget;
	GList * lst;
	
	WIDGET_GET_EWIDGET ( dyn, ewidget );	

	lst = afc_dynamic_class_get_var ( dyn, "egui_items" );
	gtk_combo_set_popdown_strings ( GTK_COMBO ( ewidget->gtk_handle ), lst );
	
	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_combo_method_set_text ( DynamicClass * dyn )
{
	EasyGUIWidgetData * ewidget;
	char * w;
	short  add;
	char * s;

	WIDGET_GET_EWIDGET ( dyn, ewidget );
	w = afc_array_master_first ( dyn->args );
	add = ( short ) ( int ) afc_array_master_next ( dyn->args );

	s = afc_string_dup ( w );
	if ( s == NULL ) return ( AFC_LOG_FAST ( AFC_ERR_NO_MEMORY ) );

	if ( add ) 
	{
		afc_easygui_internal_combo_add_unique ( dyn, ewidget, s, TRUE );
		afc_easygui_combo_method_show ( dyn );
	}

	gtk_entry_set_text ( GTK_ENTRY ( GTK_COMBO ( ewidget->gtk_handle )->entry ), s );

	afc_string_delete ( s );

	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_combo_method_get_text ( DynamicClass * dc )
{
	EasyGUIWidgetData * ewidget = afc_dynamic_class_get_var ( dc, "ewidget" );

	dc->result = ( void * ) gtk_entry_get_text ( GTK_ENTRY ( GTK_COMBO ( ewidget->gtk_handle )->entry ) );		

	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_combo_method_unique ( DynamicClass * dyn )
{
	char * w;
	EasyGUIWidgetData * ewidget;
	short c;

	w = afc_array_master_first ( dyn->args );
	c = ( short ) ( int ) afc_array_master_next ( dyn->args );

	WIDGET_GET_EWIDGET ( dyn, ewidget );	

	return ( afc_easygui_internal_combo_add_unique ( dyn, ewidget, w, c ) );
}

// INTERNAL USE ONLY
static int afc_easygui_internal_combo_add_unique ( DynamicClass * dyn, EasyGUIWidgetData * ewidget, char * text, short case_sense )
{
	struct unique_search usearch;
	GList * lst;
	GList * res;

	lst = afc_dynamic_class_get_var ( dyn, "egui_items" );

	usearch.str = text;
	usearch.case_sensitive = case_sense;

	res = g_list_find_custom ( lst, & usearch, afc_easygui_internal_combo_search_string );
	if ( res != NULL ) return ( AFC_ERR_NO_ERROR );

	lst = g_list_append ( lst, afc_string_dup ( text ) );
	afc_dynamic_class_set_var ( dyn, AFC_DYNAMIC_CLASS_VAR_KIND_POINTER, "egui_items", lst );

	return ( AFC_ERR_NO_ERROR );
}


static void afc_easygui_internal_combo_free_string ( gpointer data, gpointer user_data )
{
	if ( data ) afc_string_delete ( data );
}

static int afc_easygui_internal_combo_search_string ( gconstpointer str, gconstpointer us )
{
	struct unique_search * usearch = ( struct unique_search * ) us;

	if ( usearch->case_sensitive )
		return ( strcmp ( ( char * ) str, usearch->str ) );
	else
		return ( strcasecmp ( ( char * ) str, usearch->str ) );
}
